const db = wx.cloud.database()
const allHeros = db.collection("allHeros")
const newsPost = db.collection("newPost")
const _ = db.command
const limit = 20
const getHeroOrderByType = async (type) => {

  let {
    data
  } = await allHeros.where(_.or([{
      hero_type: _.eq(type)
    },
    {
      hero_type2: _.eq(type)
    }
  ])).orderBy("hero_id", "desc").get()
  // 因为知道所有类型的数据都不超过40条 所以只要重复一次
  if(data.length ==20){
    let arr = await allHeros.where(_.or([{
      hero_type: _.eq(type)
    },
    {
      hero_type2: _.eq(type)
    }
  ])).skip(limit).orderBy("hero_id", "desc").get();
  data = [...data,...arr.data]
  }
  
  return data
}
const getNewsPost = async ()=>{
  let {data} = await newsPost.orderBy("id","asc").get()
  return data
}
const updataNewsPost= async (id,support,supported)=>{
  await newsPost.where({
    id:_.eq(id)
  }).update({ 
    data: {
      support:support,
      supported:!supported
    }
  })
  // .then(res =>{
  //   console.log(res);
  // })
}

const allOccupation = {
  getHeroOrderByType,
  getNewsPost,
  updataNewsPost
}

module.exports = allOccupation